package com.example.task;

import com.example.pojo.entity.Orders;
import com.example.service.AlipayService;
import com.example.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class AlipayTask {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private AlipayService alipayService;

    @Autowired
    private RedisTemplate redisTemplate;

    private boolean a = false;

//    从第1分钟开始每隔30秒查询一次,查询表中五分钟内未支付的订单
//    @Scheduled(fixedRate=30000)
    public void orderConfirm(Integer id){
        log.info("订单查询定时任务...");
        List<Orders> ordersList = orderInfoService.getNoPayOrderByDuration(id,5);

        for (Orders o : ordersList) {
            Long ordNum = o.getOrdNumber();
            log.warn("超时订单----->" + ordNum);

//            核实支付宝端的订单状态,调用支付宝查单接口
            alipayService.checkOrdStatus(ordNum);
        }
    }


    @Scheduled(fixedRate=30000)
    public void order(){
        ValueOperations vop = redisTemplate.opsForValue();
        for (int i=1;i<=100;i++){
            if(vop.get("AliTime"+i)!=null && ((Long.parseLong(String.valueOf(System.currentTimeMillis())) - Long.parseLong(String.valueOf(vop.get("AliTime" + i)))) >= 60000)){
                log.info("订单查询定时任务...");
                List<Orders> ordersList = orderInfoService.getNoPayOrderByDuration(i,5);

                for (Orders o : ordersList) {
                    Long ordNum = o.getOrdNumber();
                    log.warn("超时订单----->" + ordNum);

//            核实支付宝端的订单状态,调用支付宝查单接口
                    alipayService.checkOrdStatus(ordNum);
                }
            }
        }
    }
}
